package com.iotsz.device.controller;

import com.iotsz.device.bean.Device;
import com.iotsz.device.service.DeviceService;
import com.iotsz.user.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

@Controller
public class DeviceController {

    @Autowired
    private DeviceService deviceService;


    @ResponseBody
    @GetMapping("/hello")
    public String Hello() {
        return "hello device";
    }

    @ResponseBody
    @GetMapping("/device")
    public Device getDevice(@RequestParam("id") Integer id) {
        return deviceService.getDevice(id);
    }

    @ResponseBody
    @GetMapping("/device/{id}/manage-user")
    public Map<Device, User> getDeviceUserInfo(@PathVariable("id") Integer id) {
        return deviceService.getDeviceUser(id);
    }

    @ResponseBody
    @GetMapping("/device/delete")
    public String delete(Integer id) {
        if(deviceService.deleteByPrimary(id) != 0){
            return "删除成功！";
        }
        return "删除失败！";
    }

    @ResponseBody
    @GetMapping("/device/insert")
    public String add(@RequestBody Device device) {
        if(deviceService.insertByDevice(device) != 0){
           return "新增成功~";
        }
        return "新增失败~";
    }

    @ResponseBody
    @GetMapping("/device/update")
    public String update(@RequestBody Device device) {
        if(deviceService.updateByDevice(device) != 0){
            return "更新成功~";
        }
        return "更新失败~";
    }
}
